use "..\Stata\Twelve.dta"


* polychoric
polychoric v386-v393 v397-v400
display r(sum_w)
global N = r(sum_w)
matrix r = r(R)
factormat r, n($N) forcepsd

predict pcr1 pcr2

* install labloadingplot
net from http://www.radyakin.org/stata/labloadingplot/


* loadingplot
loadingplot, msize(tiny) mlabs(vsmall)
labloadingplot, msize(tiny) mlabs(vsmall)
labloadingplot, msize(small) mcolor (red) mlabs(vsmall) xline(0, lwidth(vthin) lpattern(dash) lcolor(blue)) yline(0, lwidth(vthin) lpattern(dash) lcolor(blue)) title ("")

* lcoreplot
scoreplot, mlabel(party) msize (tiny) mlabs(tiny)
scoreplot, mlabel(surname) msize (tiny) mlabs(tiny)
scoreplot, mlabel(vip) mlabc(red) msize (tiny) mlabs(small)


** Figure 1 - Separate by party
separate pcr2, by(party) veryshortlabel 
sc pcr2? pcr1,  mcolor(blue ltblue green gray red gold dkgreen orange) msymbol(O o Oh X D T S +) msize(medsmall medlarge medsmall large medsmall medsmall medsmall large) legend(r(1)) ytitle(`: var label pcr2') scheme(s2mono)

** Figure A.4 - A curvilinear association?
reg pcr2 c.pcr1##c.pcr1
margins, at (pcr1=(-1(0.1)1))
marginsplot, recast (line) recastci(rline) ciopts(lpattern(dash))

** pcr2 depends on number of meaningful?
reg pcr2 meaningful
reg pcr2 meaningful i.partynum


** Figure A.5 - Separate by party_role
drop pcr2?*
separate pcr2, by(party_role) veryshortlabel 
sc pcr2?* pcr1,  mcolor(navy midblue ltblue gray lime green yellow red dkgreen orange ) mlabel(vip vip vip vip vip vip vip vip vip vip vip vip vip vip vip vip) msize (small small small small small small small small small small small small) mlabc(black black black black black black black black black black black black) legend(r(1)) ytitle(`: var label pcr2')

** Figure A.6 - Separate by country of election
drop pcr2?*
separate pcr2, by( country_name ) veryshortlabel 
sc pcr2? pcr1,  mcolor(midblue mint orange green)  legend(r(3)) ytitle(`: var label pcr2')

* Figure A.7 - Separate by Number of Ayes
drop pcr2?*
separate pcr2, by( count12_pro ) veryshortlabel 
sc pcr2? pcr1,  mcolor(back gray dimgray navy midblue green ltblue red orange gold yellow)  legend(r(3)) ytitle(`: var label pcr2')


** Figure 2

* First normalize dividing for 2sd (Gelman 2008)
foreach var in PopulationDensity DeprivedNone Degree age15_24 BornUK Christian Routine IndManufacturing leave  {
      sum `var'
	  generate `var'_norm = `var'/(2*r(sd))
  }

  * A) regress on PCR1

foreach var in PopulationDensity_norm DeprivedNone_norm Degree_norm age15_24_norm BornUK_norm Christian_norm Routine_norm IndManufacturing_norm leave_norm leavedummy {
     quietly regress pcr1  i.partynum `var', robust
      estimates store `var'_1	  
  }

  
* B) regress on PCR2
		
foreach var in PopulationDensity_norm DeprivedNone_norm Degree_norm age15_24_norm BornUK_norm Christian_norm Routine_norm IndManufacturing_norm leave_norm leavedummy {
      quietly regress pcr2  i.partynum `var', robust
      estimates store `var'_2
  }
		
* C) Plot coefficients
		
coefplot (PopulationDensity_norm_1 DeprivedNone_norm_1 Degree_norm_1 age15_24_norm_1 BornUK_norm_1 Christian_norm_1 Routine_norm_1 IndManufacturing_norm_1, label(Census) mcolor(navy) ciopts(lcolor(navy))) ///
		(leave_norm_1 leavedummy_1, label(Referendum) msymbol(S) mcolor(red) ciopts(lcolor(red))) , bylabel(Factor 1)  ///
        keep (PopulationDensity_norm DeprivedNone_norm Degree_norm age15_24_norm BornUK_norm Christian_norm Routine_norm IndManufacturing_norm leave_norm leavedummy)  ///
		xline(0, lcolor(black) lp (dash) ) || ///
		(PopulationDensity_norm_2 DeprivedNone_norm_2 Degree_norm_2 age15_24_norm_2 BornUK_norm_2 Christian_norm_2 Routine_norm_2 IndManufacturing_norm_2, label(Census)) ///
		(leave_norm_2 leavedummy_2, label(Referendum)), bylabel(Factor 2)  ///
        keep (PopulationDensity_norm DeprivedNone_norm Degree_norm age15_24_norm BornUK_norm Christian_norm Routine_norm IndManufacturing_norm leave_norm leavedummy) scheme(s2mono)
		

** Figure A.8 (Table A.3)

* A) regress on PCR1

foreach var in PopulationDensity DeprivedNone Degree age15_24 BornUK Christian Routine IndManufacturing leave leavedummy {
      quietly regress pcr1  i.partynum `var', robust
      estimates store `var'_1	  
  }

  
* B) regress on PCR2
		
foreach var in PopulationDensity DeprivedNone Degree age15_24 BornUK Christian Routine IndManufacturing leave leavedummy {
      quietly regress pcr2  i.partynum `var', robust
      estimates store `var'_2
  }
		
* C) Plot coefficients
		
coefplot (PopulationDensity_1 DeprivedNone_1 Degree_1 age15_24_1 BornUK_1 Christian_1 Routine_1 IndManufacturing_1, label(Census) mcolor(navy) ciopts(lcolor(navy))) ///
		(leave_1 leavedummy_1, axis(2) label(Referendum) msymbol(S) mcolor(red) ciopts(lcolor(red))) , bylabel(Factor 1)  ///
        keep (PopulationDensity DeprivedNone Degree age15_24 BornUK Christian Routine IndManufacturing leave leavedummy)  ///
		xline(0, lcolor(navy) )  xline(0, lcolor(red) lp(dash) axis(2)) xscale(range(-1.3 0.8) axis(2)) xlabel(#7, axis(2))   || ///
		(PopulationDensity_2 DeprivedNone_2 Degree_2 age15_24_2 BornUK_2 Christian_2 Routine_2 IndManufacturing_2, label(Census)) ///
		(leave_2 leavedummy_2, axis(2) label(Referendum)), bylabel(Factor 2)  ///
        keep (PopulationDensity DeprivedNone Degree age15_24 BornUK Christian Routine IndManufacturing leave leavedummy) scheme(s2mono)
		
		
* Heteroskedastic regressions

* Test for heteroskedasticity

regress pcr1 i.partynum c.leave##c.margin17
hettest
whitetst


** Figure 3 - Table A.5

hetregress pcr1 i.partynum c.leave##c.margin17, het(elected)
margins, dydx(leave) at(margin17 =(0(1)77)) vsquish
marginsplot, recast (line) recastci(rline) ciopts(lpattern(dash)) yline(0, lcolor(red)) ytitle("Marginal Effect of Leave results", axis(1)) title("") ///
addplot(hist margin17, percent yaxis(2) bin(20) lwidth(vvthin) color(none) lcolor(gs10) yscale(alt axis(2)))  ///
legend(off) scheme(s2mono) graphregion(fcolor(white)) xsca(titlegap(2)) xtitle(Electoral margin 2017) title("")

*for the residuals
predict predictedpcr1
gen resid=pcr1-predictedpcr1
gen absresid=abs(resid)


* Figure A.9 - Table A.5

hetregress pcr1 i.partynum c.leave##c.elected, het(margin17)
margins, dydx(leave) at(elected =(1970(1)2018)) vsquish
marginsplot, recast (line) recastci(rline) ciopts(lpattern(dash)) yline(0, lcolor(red)) ytitle("Marginal Effect of Leave results", axis(1)) title("") ///
addplot(hist elected, percent yaxis(2) bin(20) lwidth(vvthin) color(none) lcolor(gs10) yscale(alt axis(2)))  ///
legend(off) scheme(s2mono) graphregion(fcolor(white)) xsca(titlegap(2)) xtitle(Year of first election) title("")


* The cost of not being responsive and of compromising

** Table 2
reg change1917 absresid leave turnoutchange1917 margin17 i.partynum, robust
linktest
ovtest
reg change1917 absresid leave turnoutchange1917 margin17 if party=="C", robust
reg change1917 absresid leave turnoutchange1917 margin17 if party=="L", robust
reg changecon pcr1 margin17 leave turnoutchange1917 if party=="C", robust
reg changelab pcr1 leave margin17 turnoutchange1917 if party=="L", robust


* Table A.6
reg change1917 absresid leave margin17 turnoutchange1917 if leavedummy==1 & party=="C", robust
reg change1917 absresid leave margin17 turnoutchange1917 if leavedummy==1 & party=="L", robust
reg change1917 absresid leave margin17 turnoutchange1917 if leavedummy==0 & party=="C", robust
reg change1917 absresid leave margin17 turnoutchange1917 if leavedummy==0 & party=="L", robust

	
* Table A.7
reg changecon pcr1 leave margin17 turnoutchange1917 if party=="C" & substituted==0, robust
reg changecon pcr1 leave margin17 turnoutchange1917 if party=="C" & substituted==1, robust





		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		


